<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="关联第三方id" prop="chatId">
      <el-input v-model="dataForm.chatId" placeholder="关联第三方id"></el-input>
    </el-form-item>
    <el-form-item label="1.手机号注册   2.微信注册 3.qq注册4微博" prop="regType">
      <el-input v-model="dataForm.regType" placeholder="1.手机号注册   2.微信注册 3.qq注册4微博"></el-input>
    </el-form-item>
    <el-form-item label="1真实用户   0  假用户" prop="dataType">
      <el-input v-model="dataForm.dataType" placeholder="1真实用户   0  假用户"></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="设备类型0ios1安卓2ios3h5端4pc端" prop="deviceType">
      <el-input v-model="dataForm.deviceType" placeholder="设备类型0ios1安卓2ios3h5端4pc端"></el-input>
    </el-form-item>
    <el-form-item label="机型" prop="modelType">
      <el-input v-model="dataForm.modelType" placeholder="机型"></el-input>
    </el-form-item>
    <el-form-item label="设备id" prop="deviceId">
      <el-input v-model="dataForm.deviceId" placeholder="设备id"></el-input>
    </el-form-item>
    <el-form-item label="账户状态 0不可用  1可用" prop="state">
      <el-input v-model="dataForm.state" placeholder="账户状态 0不可用  1可用"></el-input>
    </el-form-item>
    <el-form-item label="版本号" prop="version">
      <el-input v-model="dataForm.version" placeholder="版本号"></el-input>
    </el-form-item>
    <el-form-item label="最后一次登陆时间" prop="lastLoginTime">
      <el-input v-model="dataForm.lastLoginTime" placeholder="最后一次登陆时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: 0,
          userName: '',
          password: '',
          chatId: '',
          regType: '',
          dataType: '',
          updateTime: '',
          createTime: '',
          deviceType: '',
          modelType: '',
          deviceId: '',
          state: '',
          version: '',
          lastLoginTime: ''
        },
        dataRule: {
          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          chatId: [
            { required: true, message: '关联第三方id不能为空', trigger: 'blur' }
          ],
          regType: [
            { required: true, message: '1.手机号注册   2.微信注册 3.qq注册4微博不能为空', trigger: 'blur' }
          ],
          dataType: [
            { required: true, message: '1真实用户   0  假用户不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          deviceType: [
            { required: true, message: '设备类型0ios1安卓2ios3h5端4pc端不能为空', trigger: 'blur' }
          ],
          modelType: [
            { required: true, message: '机型不能为空', trigger: 'blur' }
          ],
          deviceId: [
            { required: true, message: '设备id不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '账户状态 0不可用  1可用不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号不能为空', trigger: 'blur' }
          ],
          lastLoginTime: [
            { required: true, message: '最后一次登陆时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.userId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/simida/userdata/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userName = data.userData.userName
                this.dataForm.password = data.userData.password
                this.dataForm.chatId = data.userData.chatId
                this.dataForm.regType = data.userData.regType
                this.dataForm.dataType = data.userData.dataType
                this.dataForm.updateTime = data.userData.updateTime
                this.dataForm.createTime = data.userData.createTime
                this.dataForm.deviceType = data.userData.deviceType
                this.dataForm.modelType = data.userData.modelType
                this.dataForm.deviceId = data.userData.deviceId
                this.dataForm.state = data.userData.state
                this.dataForm.version = data.userData.version
                this.dataForm.lastLoginTime = data.userData.lastLoginTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/simida/userdata/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'userName': this.dataForm.userName,
                'password': this.dataForm.password,
                'chatId': this.dataForm.chatId,
                'regType': this.dataForm.regType,
                'dataType': this.dataForm.dataType,
                'updateTime': this.dataForm.updateTime,
                'createTime': this.dataForm.createTime,
                'deviceType': this.dataForm.deviceType,
                'modelType': this.dataForm.modelType,
                'deviceId': this.dataForm.deviceId,
                'state': this.dataForm.state,
                'version': this.dataForm.version,
                'lastLoginTime': this.dataForm.lastLoginTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
